<?php
namespace app\admin\controller;
use app\common\controller\AdminBase;
/*区域门禁管理*/
class SlidingDoor extends AdminBase{
	protected $beforeActionList = [
        'before_index'=>['only'=>'index'],
    ];
	function _initialize() {
		parent::_initialize();  
    }
	/*区域门禁列表*/
	function index(){
		echo $this->fetch();
	}	
	//删除区域门禁
	function delete(){
		$id=input("id");
		$shop_id=sp_get_current_shop_id();
		$flag=db("SlidingGroup")->where(" shop_id=$shop_id and  door_ids like '%,$id,%'")->count(1);
		if($flag>0){//区域门禁已分配
			$this->ajax_error("区域门禁在权限分组已使用，无法删除");	
		}
		db("SlidingDoor")->where("id =$id")->delete();
		$this->ajax_success("成功");
	}
	function edit(){
		$model=db("SlidingDoor")->find(input('id'));
		$this->assign("model",$model);
		echo $this->fetch();	
	}
	function edit_post(){
		if(request()->isPost()){//提交
			db("SlidingDoor")->update(array("id"=>input("id"),"name"=>input("name"),"desc"=>input("desc"),  "verify_type" => input("verify_type")));
			$this->ajax_success("成功");
		}
	}
	
	/*卡列表数据*/
	function ajax_index(){
		$this->ajax($this->_ajax_index());
	}
	
	function add(){
		echo $this->fetch();
	}
	function add_post(){
		if(request()->isPost()){
			$shop_id=sp_get_current_shop_id();
			$flag=db("SlidingDoor")->where("code='".input('code')."' and shop_id=$shop_id ")->count(1);
			if($flag>0){
				$this->ajax_error("编号已经存在");
			}else{
				db("SlidingDoor")->insert(array("name"=>input("name"),"desc"=>input("desc"),"code"=>input("code"),'shop_id'=>$shop_id, "verify_type" => input("verify_type")));
				$this->ajax_success("成功");
			}
		}
	}
	
	///////////////////////////////////////////////////////////////////////////////////
	/**
	 * 权限菜单
	 **/
	function before_index(){
		$auth_group=sp_get_auth_menu_3("10041005");
		$this->assign("menus",$auth_group);
	}
	
	/*获取卡数据*/
	private function _ajax_index(){
		$query=array(
		 'name'=>array("field" =>"name","operator" =>"like"),
		 );
		$shop_id=sp_get_current_shop_id();
		$draw=input("draw",0)+1;//请求时间		 
		$where_and=join(" and ",sp_get_param_sql(request()->isPost(),$query));
		
		$where_and.=" and shop_id=$shop_id";
		
		$total=db("SlidingDoor")->where($where_and)->fetchsql(FALSE)->count(1);
		$list=db("SlidingDoor")->where($where_and)->order($this->get_order())->limit($this->get_limit())->fetchsql(FALSE)->select();
		return array('pageData'=>$list,'total'=>$total,"draw"=>$draw);
	}
	
}
